package Stackdemo;

import java.util.Stack;

public class Minstack {
    Stack<Integer> stack;
    Stack<Integer> minstack;

    public Minstack() {
        stack = new Stack<>();
        minstack= new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if(minstack.empty()){
            minstack.push(val);
        }else{
            int temp=minstack.peek();
            if(val<=temp){
                minstack.push(val);
            }
        }
    }

    public void pop() {
        if(stack.empty()||minstack.empty()){
            return;
        }else{
            int temp=stack.pop();
            if(temp==minstack.peek()){
                minstack.pop();
            }
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        if(minstack.empty()){
            return 0;
        }
        return minstack.peek();
    }
}
